package com.yunhui.wx.robot.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunhui.wx.robot.common.BaseCommon;
import com.yunhui.wx.robot.service.TlRobotService;

@Controller
public class TlRobotController {

	
	@PostMapping("/tuling")
	@ResponseBody
	public Map<String,Object> post(HttpServletRequest request,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> result=new HashMap<>();
		String res = TlRobotService.getTextResponse(request.getParameter("userid"), request.getParameter("info"));
		if(res!=null && res!=""){
			result.put("code", 100000);
			result.put("text", res);
		}
		return result;
	}
	
	
	@PostMapping("/tuling/v2")
	@ResponseBody
	public Map<String,Object> postv2(HttpServletRequest request,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> result=new HashMap<>();
		String res = TlRobotService.getTextResponseV2(request.getParameter("userid"), request.getParameter("info"));
		if(res!=null && res!=""){
			result.put("code", 100000);
			result.put("text", res);
		}
		return result;
	}
	
	@GetMapping("/tuling")
	@ResponseBody
	public String get(@RequestParam("callback")String callback,HttpServletRequest request) throws JsonProcessingException{
		String res = TlRobotService.getTextResponse(BaseCommon.TL_ROBOT_USER_ID, request.getParameter("text"));
		ObjectMapper mapper=new ObjectMapper();
		Map<String,String> result=new HashMap<>();
		result.put("res", res);
		return callback+"("+mapper.writeValueAsString(result)+")";
	}
}
